﻿using System.Collections.Generic;
using System.Windows.Forms;
using ApplicationLibrary.ExtendedControls;
using ApplicationLibrary.SqlConnector;

namespace Vintner.Masters.ModuleData
{
    internal class RadarCustomerParam : RadarParameters
    {
        public RadarCustomerParam(string code)
        {
            Params = new List<object>();
            Params.Add(code);
        }
    }

    internal class RadarDescriptionsParam : RadarParameters
    {
        public RadarDescriptionsParam(DescriptionType type, string code)
        {
            Params = new List<object>();
            Params.Add(type);
            Params.Add(code);
        }
    }

    internal class RadarTaxCodesParam : RadarParameters
    {
        public RadarTaxCodesParam(string code)
        {
            Params = new List<object>();
            Params.Add(code);
        }
    }

    #region Radar RadarCustomerForm

    internal class RadarCustomerForm : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1, p2;

        public RadarCustomerForm()
            : base()
        {
            rdrCodeColumn = VI_Customers.Customer.Name;
            rdrDescColumn = VI_Customers.Description.Name;
        }

        protected override Table GetRadarTable()
        {
            return new VI_Customers();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            p1 = new SqlABParameter("@p1", VI_Customers.Customer);

            qb.Clear();
            qb.AddSelectAll(VI_Customers.Name);
            qb.AddFrom(VI_Customers.Name);
            qb.AddCompare(VI_Customers.Customer, p1);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param[0];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[VI_Customers.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            p2 = new SqlABParameter("@p2", VI_Customers.Disabled);
            qb.Clear();
            qb.AddSelectAll(VI_Customers.Name);
            qb.AddFrom(VI_Customers.Name);
            qb.AddFilter(findQuery);
            qb.AddCompare(VI_Customers.Disabled, p2);
            qb.AddOrder(VI_Customers.Description);

            sqlCmd.Parameters.Add(p2);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
            p2.Value = 0;
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            string Customer = DataGridViewRowValues.GetValue<string>(row, VI_Customers.Customer);
            return new RadarCustomerParam(Customer);
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarCustomerParam(text);
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(0);
        }
    }

    #endregion

    #region Radar TaxCodes

    internal class RadarTaxCodes : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1;
        private SqlABParameter p2;

        public RadarTaxCodes()
            : base()
        {
            rdrCodeColumn = VI_TaxCode.Code.Name;
            rdrDescColumn = VI_TaxCode.Description.Name;
            rdrNameSpace = "Vintner.Vintner.Sales.Forms.TaxCodes";
        }

        protected override Table GetRadarTable()
        {
            return new VI_TaxCode();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            p1 = new SqlABParameter("@p1", VI_TaxCode.Code);

            qb.Clear();
            qb.AddSelectAll(VI_TaxCode.Name);
            qb.AddFrom(VI_TaxCode.Name);
            qb.AddCompare(VI_TaxCode.Code, p1);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param[0];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[VI_TaxCode.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            p2 = new SqlABParameter("@p2", VI_TaxCode.Code);
            qb.Clear();
            qb.AddSelectAll(VI_TaxCode.Name);
            qb.AddFrom(VI_TaxCode.Name);
            qb.AddFilter(findQuery);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
            p2.Value = VI_TaxCode.Code.DefaultValue;
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarTaxCodesParam(DataGridViewRowValues.GetValue<string>(row, VI_TaxCode.Code));
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarTaxCodesParam(text);
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(0);
        }
    }

    #endregion

    #region Radar Descriptions

    internal class RadarDescriptions : RadarForm
    {
        private QueryBuilder qb = new QueryBuilder();
        private SqlABParameter p1, p2, p3;
        private DescriptionType type;

        public RadarDescriptions(DescriptionType type)
            : base()
        {
            this.type = type;
            rdrCodeColumn = VI_Descriptions.Code.Name;
            rdrDescColumn = VI_Descriptions.Description.Name;
            switch (type)
            {
                case DescriptionType.E_Title:
                    rdrNameSpace = "Vintner.Vintner.Masters.Forms.Titles";
                    break;

                case DescriptionType.E_Category:
                    rdrNameSpace = "Vintner.Vintner.Masters.Forms.Categories";
                    break;

                case DescriptionType.E_Payment:
                    rdrNameSpace = "Vintner.Vintner.Masters.Forms.Payments";
                    break;

                case DescriptionType.E_Appearance:
                    rdrNameSpace = "Vintner.Vintner.Masters.Forms.Appereance";
                    break;

                case DescriptionType.E_Transport:
                    rdrNameSpace = "Vintner.Vintner.Masters.Forms.Transport";
                    break;

                default:
                    rdrNameSpace = "Vintner.Vintner.Masters.Forms.Descriptions";
                    break;
            }
        }

        protected override Table GetRadarTable()
        {
            return new VI_Descriptions();
        }

        protected override bool DefineFindQuery(SqlABCommand sqlCmd)
        {
            qb.Clear();
            qb.AddSelectAll(VI_Descriptions.Name);
            qb.AddFrom(VI_Descriptions.Name);
            qb.AddCompare(VI_Descriptions.Type, "@p1");
            qb.AddCompare(VI_Descriptions.Code, "@p2");

            p1 = new SqlABParameter("@p1", VI_Descriptions.Type);
            p2 = new SqlABParameter("@p2", VI_Descriptions.Code);

            sqlCmd.CommandText = qb.Query;
            sqlCmd.Parameters.Add(p1);
            sqlCmd.Parameters.Add(p2);

            return true;
        }

        protected override void PrepareFindQuery(IRadarParameters param)
        {
            p1.Value = param.Params[0];
            p2.Value = param.Params[1];
        }

        protected override void OnFound(SqlABDataReader sqlReader)
        {
            Description = sqlReader[VI_Descriptions.Description.Name].ToString();
        }

        protected override string DefineBrowseQuery(SqlABCommand sqlCmd, string findQuery)
        {
            p3 = new SqlABParameter("@p3", VI_Descriptions.Type);

            qb.Clear();
            qb.AddSelectAll(VI_Descriptions.Name);
            qb.AddFrom(VI_Descriptions.Name);
            qb.AddCompare(VI_Descriptions.Type, "@p3");
            qb.AddFilter(findQuery);

            sqlCmd.Parameters.Add(p3);

            return qb.Query;
        }

        protected override void PrepareBrowseQuery()
        {
            p3.Value = type;
        }

        protected override IRadarParameters PrepareRadarParameters(DataGridViewRow row)
        {
            return new RadarDescriptionsParam(type, DataGridViewRowValues.GetValue<string>(row, VI_Descriptions.Code));
        }

        public override IRadarParameters GetRadarParameters(string text)
        {
            return new RadarDescriptionsParam(type, text);
        }

        public override string GetCodeFromParameters(IRadarParameters param)
        {
            return param.GetValue<string>(1);
        }
    }

    #endregion
}